home *** CD-ROM | disk | FTP | other *** search
- #ifdef __MWERKS__
- #include <console.h>
- #endif
- #include <stdio.h>
-
- #include "Wide.h"
-
- /* forward references */
- void tst_printint( wide *big_ptr );
- void tst_print3( wide *big1_ptr, wide *big2_ptr, wide *big3_ptr );
- void tst_compare3( wide *big1_ptr, wide *big2_ptr, wide *big3_ptr,
- wide *save1_ptr, wide *save2_ptr, wide *save3_ptr, short equal_test );
- void tst_wide( wide *big_ptr, long hi, long lo, char *strp );
- void __assertprint(char *filenam_ptr, char *linenum_ptr, char *expression_ptr);
- void __assert_custom(char *filenam_ptr, char *linenum_ptr, char *expression_ptr);
- void tst_long( long actual, long expected, char *strp );
-
-
-
-
- main()
- {
- short i;
- short tst1, tst2, tst3;
- wide big1, big2, big3;
- wide save1, save2, save3;
- wide *resultp, *resultAp, *resultBp;
- decimal dec_str;
- long result, remainder;
-
- wide wide1;
- decimal dec;
-
-
- wide1.hi = 0;
- wide1.lo = 0x00000401;
- WideShift(&wide1, 3);
-
- wide1.hi = 0;
- wide1.lo = 0x00000C;
- WideShift(&wide1, 3); // Should be 2 because of rounding
-
-
- wide1.hi = 0;
- wide1.lo = 0;
- WideToDecStr(&dec, &wide1); /* should be "0" */
-
- wide1.hi = 0;
- wide1.lo = 1;
- WideToDecStr(&dec, &wide1); /* should be "1" */
-
- WideNegate( &wide1 );
- WideToDecStr(&dec, &wide1); /* should be Negative "1" */
-
- wide1.hi = 0;
- wide1.lo = 12345;
- WideToDecStr(&dec, &wide1); /* should be "12345" */
-
- WideNegate( &wide1 );
- WideToDecStr(&dec, &wide1); /* should be Negative "12345" */
-
- wide1.hi = 1;
- wide1.lo = 0;
- WideToDecStr(&dec, &wide1); /* should be "4294967296" */
-
- WideNegate( &wide1 );
- WideToDecStr(&dec, &wide1); /* should be Negative "4294967296" */
-
-
- resultp = WideAssign32( &big1, -1 );
- WideAssign32( &big2, 0 );
- WideAssign32( &big3, 25 );
- save1 = big1;
- save2 = big2;
- save3 = big3;
- printf( "WideAssign32() = " );
- tst_print3( &big1, &big2, &big3 );
-
- WideAdd32( &big1, 3 );
- resultAp = WideAdd32( &big2, -1 );
- WideAdd32( &big3, -30 );
- printf( "WideAdd32() = " );
- tst_print3( &big1, &big2, &big3 );
-
- WideSubtract32( &big1, 3 );
- WideSubtract32( &big2, -1 );
- resultBp = WideSubtract32( &big3, -30 );
- printf( "WideSubtract32() = " );
- tst_print3( resultp, resultAp, resultBp );
- tst_compare3( resultp,resultAp, resultBp, &save1, &save2, &save3, true );
-
- WideAssign32( &big1, 0x40000000 );
- printf( "WideAssign32() = " );
- tst_printint( &big1 ); printf( "\n" );
- WideBitShift( &big1, -2 );
- printf( "WideBitShift(left twice) = " );
- tst_printint( &big1 ); printf( "\n" );
- WideAdd32( &big1, 3507 );
- printf( "WideAdd32(3507)\n\n" );
- WideToDecStr( &dec_str, &big1 );
- if( (dec_str.sgn == 0) &&
- (dec_str.exp == 0) &&
- (dec_str.sig.length == 10) &&
- (dec_str.sig.text[0] == '4') &&
- (dec_str.sig.text[1] == '2') &&
- (dec_str.sig.text[2] == '9') &&
- (dec_str.sig.text[3] == '4') &&
- (dec_str.sig.text[4] == '9') &&
- (dec_str.sig.text[5] == '7') &&
- (dec_str.sig.text[6] == '0') &&
- (dec_str.sig.text[7] == '8') &&
- (dec_str.sig.text[8] == '0') &&
- (dec_str.sig.text[9] == '3') )
- printf( "PASS" );
- else
- printf( "FAIL" );
- printf( ": " );
- tst_printint( &big1 );
- printf( ", WideToDecStr() with more than 32 bits\n\n" );
-
- WideAssign32( &big1, -10 );
- WideAssign32( &big2, 0x7FFFFFF0 );
- WideAssign32( &big3, 0x7FFFFFFF );
- save1 = big1;
- save2 = big2;
- save3 = big3;
- printf( "WideAssign32() = " );
- tst_print3( &big1, &big2, &big3 );
-
- WideAdd32( &big3, 1 );
- resultp = (wide *)WideAdd( &big3, &big1 );
- for( i=0; i < 20; ++i ) WideAdd( &big3, &big2 );
- printf( "WideAdd() = " );
- tst_print3( &big1, &big2, resultp );
- tst_compare3( &big1, &big2, resultp, &save1, &save2, &save3, false );
-
- /*------------------------------*/
- printf( "<<< press ENTER for more >>>\n\n" );
- getchar();
-
- WideSubtract32( &big3, 1 );
- resultp = (wide *)WideSubtract( &big3, &big1 );
- for( i=0; i < 20; ++i ) WideSubtract( &big3, &big2 );
-
- printf( "WideSubtract() = " );
- tst_print3( &big1, &big2, resultp );
- tst_compare3( &big1, &big2, resultp, &save1, &save2, &save3, true );
-
- WideAssign32( &big1, 0 );
- WideAssign32( &big2, 7 );
- WideAssign32( &big3, 0x7FFFFFFF );
- save1 = big1;
- save2 = big2;
- save3 = big3;
- printf( "WideAssign32() = " );
- tst_print3( &big1, &big2, &big3 );
-
- WideNegate( &big1 );
- WideNegate( &big2 );
- WideNegate( &big3 );
- printf( "WideNegate() = " );
- tst_print3( &big1, &big2, &big3 );
- printf( "\n" );
-
- WideAssign32( &big3, 1 );
- save3 = big3;
- WideAdd( &big2, &big3 );
- if( (big2.lo == -6) && (big2.hi == -1) )
- printf( "PASS" );
- else
- printf( "FAIL" );
- printf( ": adding a small positive to a small negative\n" );
- WideSubtract( &big2, &big3 );
- if( (big3.lo == save3.lo) && (big3.hi == save3.hi) )
- printf( "PASS" );
- else
- printf( "FAIL" );
- printf( ": subtracting a small positive to a small negative\n\n" );
-
- WideAssign32( &big1, 18 );
- result = WideDivide( &big1, 4, &remainder );
- if( (result == 4) && (remainder == 2) )
- printf( "PASS" );
- else
- printf( "FAIL" );
- printf( ": %ld, %ld, WideDivide()\n",
- result, remainder );
-
- WideAssign32( &big1, 10000 );
- result = WideDivide( &big1, 200, &remainder );
- if( (result == 50) && (remainder == 0) )
- printf( "PASS" );
- else
- printf( "FAIL" );
- printf( ": %ld, %ld, WideDivide()\n",
- result, remainder );
-
- WideMultiply( 10, 307, &big1 );
- result = WideDivide( &big1, 307, &remainder );
- if( (result == 10) && (remainder == 0) )
- printf( "PASS" );
- else
- printf( "FAIL" );
- printf( ": %ld, %ld, WideMultiply() WideDivide(), small positive num.\n",
- result, remainder );
-
- WideMultiply( -6, -3, &big1 );
- result = WideDivide( &big1, -4, &remainder );
- if( (result == -4) && (remainder == 2) )
- printf( "PASS" );
- else
- printf( "FAIL" );
- printf( ": %ld, %ld, WideMultiply() WideDivide(), small negative num.\n",
- result, remainder );
-
- WideMultiply( 0x7FFFFFF3, 0x5A5A, &big1 );
- result = WideDivide( &big1, 0x7FFFFFF3, &remainder );
- if( (result == 0x5A5A) && (remainder == 0) )
- printf( "PASS" );
- else
- printf( "FAIL" );
- printf( ": 0x%lx, %ld, WideMultiply() WideDivide()\n",
- result, remainder );
-
- WideMultiply( -(0x7FFFFFF3), 0x5A5A, &big1 );
- result = WideDivide( &big1, 0x7FFFFFF3, &remainder );
- if( (result == -(0x5A5A)) && (remainder == 0) )
- printf( "PASS" );
- else
- printf( "FAIL" );
- printf( ": 0x%lx, %ld, WideMultiply() WideDivide()\n",
- result, remainder );
-
- /*------------------------------*/
- printf( "<<< press ENTER for more >>>\n\n" );
- getchar();
-
- WideAssign32( &big1, 1 );
- result = WideDivide( &big1, 0, NULL );
- if( result == gxPositiveInfinity )
- printf( "PASS" );
- else
- printf( "FAIL" );
- printf( ": %lx, WideDivide() positive by 0\n", result );
-
- WideAssign32( &big1, -1 );
- result = WideDivide( &big1, 0, NULL );
- if( result == gxNegativeInfinity )
- printf( "PASS" );
- else
- printf( "FAIL" );
- printf( ": %lx, WideDivide() negative by 0\n\n", result );
-
- resultp = (wide *)WideMultiply( 0, 0, &big1 );
- WideMultiply( -4, -(0x7FFFFFFF), &big2 );
- WideMultiply( -(0x7FFFFFFF), 0x7FFFFFFF, &big3 );
- save1 = big1;
- save2 = big2;
- save3 = big3;
- printf( "WideMultiply(), 0 * 0, -4 * -(0x7FFFFFFF), -(0x7FFFFFFF) * 0x7FFFFFFF =\n" );
- tst_print3( resultp, &big2, &big3 );
-
- WideNegate( &big1 );
- WideNegate( &big2 );
- WideNegate( &big3 );
- printf( "WideNegate() = " );
- tst_print3( &big1, &big2, &big3 );
- printf( "\n" );
-
- result = WideDivide( &big2, -4, NULL );
- if( result == 0x7FFFFFFF )
- printf( "PASS" );
- else
- printf( "FAIL" );
- printf( ": WideDivide() = 0x%lx\n", result );
-
- WideNegate( &big3 );
- result = WideDivide( &big3, 0x7FFFFFFF, NULL );
- if( result == -(0x7FFFFFFF) )
- printf( "PASS" );
- else
- printf( "FAIL" );
- printf( ": WideDivide() = 0x%lx\n", result );
-
- WideAssign32( &big1, 25 );
- resultp = (wide *)WideWideDivide( &big1, 3, &remainder );
- if( (resultp->lo == 8) && (resultp->hi == 0) && (remainder == 1) )
- printf( "PASS" );
- else
- printf( "FAIL" );
- printf( ": %ld, %ld, WideWideDivide()\n",
- resultp->lo, remainder );
-
- resultp = (wide *)WideMultiply( 32, 0x7FFFFFFF, &big2 );
- tst_wide( resultp, 0xF, 0xFFFFFFE0, "WideMultiply()" );
-
- resultp = (wide *)WideWideDivide( resultp, 4, &remainder );
- if( (resultp->lo == 0xFFFFFFF8) && (resultp->hi == 3) && (remainder == 0) )
- printf( "PASS" );
- else
- printf( "FAIL" );
- printf( ": 0x%lx, 0x%lx, %ld, WideWideDivide()\n",
- resultp->hi, resultp->lo, remainder );
-
- resultp = (wide *)WideWideDivide( resultp, 16, &remainder );
- if( (resultp->lo == 0x3FFFFFFF) && (resultp->hi == 0) && (remainder == 8) )
- printf( "PASS" );
- else
- printf( "FAIL" );
- printf( ": 0x%lx, 0x%lx, %ld, WideWideDivide()\n",
- resultp->hi, resultp->lo, remainder );
-
- WideAssign32( &big1, 0x7FFFFFFF );
- resultp = (wide *)WideBitShift( &big1, -2 );
- tst_wide( resultp, 1, 0xFFFFFFFC, "WideBitShift(pos, -2)" );
-
- resultp = (wide *)WideBitShift( &big1, 1 );
- tst_wide( resultp, 0, 0xFFFFFFFE, "WideBitShift(pos, 1)" );
-
- WideAssign32( &big1, -10 );
- resultp = (wide *)WideBitShift( &big1, 1 );
- tst_wide( resultp, -1, -5, "WideBitShift(neg, 1)" );
-
- big1.hi = 0x80000000;
- big1.lo = 0;
- resultp = (wide *)WideBitShift( &big1, -1 );
- tst_wide( resultp, 0, 0, "WideBitShift(max neg, -1)" );
-
- big1.hi = 0x80000000;
- big1.lo = 0;
- resultp = (wide *)WideBitShift( &big1, 1 );
- tst_wide( resultp, 0xC0000000, 0, "WideBitShift(max neg, 1)" );
-
- big1.hi = 0;
- big1.lo = 3;
- resultp = (wide *)WideShift( &big1, 1 );
- tst_wide( resultp, 0, 2, "WideShift(pos, 1)" );
-
- big1.hi = 1;
- big1.lo = 0xFFFFFFFF;
- resultp = (wide *)WideShift( &big1, 1 );
- tst_wide( resultp, 1, 0, "WideShift(pos, 1)" );
- resultp = (wide *)WideShift( &big1, -1 );
- tst_wide( resultp, 2, 0, "WideShift(pos, -1)" );
-
- /*------------------------------*/
- printf( "<<< press ENTER for more >>>\n\n" );
- getchar();
-
- big1.hi = 3;
- big1.lo = 0xC0000000;
- resultp = (wide *)WideBitShift( &big1, 33 );
- tst_wide( resultp, 0, 1, "WideBitShift(, 33)" );
-
- big1.hi = 3;
- big1.lo = 0xC0000000;
- resultp = (wide *)WideBitShift( &big1, -33 );
- tst_wide( resultp, 0x80000000, 0, "WideBitShift(, -33)" );
-
- big1.hi = 1;
- big1.lo = 1;
- resultp = (wide *)WideShift( &big1, -1 );
- tst_wide( resultp, 2, 2, "WideShift(pos, -1)" );
-
- big1.hi = -1;
- big1.lo = -20;
- resultp = (wide *)WideShift( &big1, 2 );
- tst_wide( resultp, -1, -5, "WideShift(neg, 2)" );
- resultp = (wide *)WideShift( &big1, 1 );
- tst_wide( resultp, -1, -2, "WideShift(neg, 1)" );
-
- big1.hi = 0x80000000;
- big1.lo = 0;
- resultp = (wide *)WideShift( &big1, -1 );
- tst_wide( resultp, 0, 0, "WideShift(max neg, -1)" );
-
- big1.hi = 0x80000000;
- big1.lo = 0;
- resultp = (wide *)WideShift( &big1, 1 );
- tst_wide( resultp, 0xC0000000, 0, "WideShift(max neg, 1)" );
-
- big1.hi = 0;
- big1.lo = 0x0000000C;
- resultp = (wide *)WideShift( &big1, 3 );
- tst_wide( resultp, 0, 2, "WideShift Rounding" );
-
- big1.hi = 0x00000001;
- big1.lo = 0x80000000;
- resultp = (wide *)WideShift( &big1, 32 );
- tst_wide( resultp, 0, 2, "WideShift Rounding" );
-
- big1.hi = 0x00000001;
- big1.lo = 0x00000000;
- resultp = (wide *)WideShift( &big1, 32 );
- tst_wide( resultp, 0, 1, "WideShift Rounding" );
-
- big1.hi = 0;
- big1.lo = 9;
- result = WideSquareRoot( &big1 );
- tst_long( result, 3, "WideSquareRoot(0,9)" );
-
- big1.hi = 0;
- big1.lo = 0;
- result = WideSquareRoot( &big1 );
- tst_long( result, 0, "WideSquareRoot(0,0)" );
-
- big1.hi = 4;
- big1.lo = 0;
- result = WideSquareRoot( &big1 );
- tst_long( result, 0x20000, "WideSquareRoot(4,0)" );
-
- big1.hi = 0x40000000;
- big1.lo = 0;
- result = WideScale( &big1 );
- tst_long( result, 62, "WideScale()" );
-
- return 0;
- }
-
- void tst_print3( wide *big1_ptr, wide *big2_ptr, wide *big3_ptr )
- {
- tst_printint( big1_ptr );
- printf( ", " );
- tst_printint( big2_ptr );
- printf( ", " );
- tst_printint( big3_ptr );
- printf( "\n" );
- }
-
-
- void tst_printint( wide *big_ptr )
- {
- char *outp;
- short num_dig;
- decimal dec_str;
-
-
- WideToDecStr( &dec_str, big_ptr );
-
- num_dig = dec_str.sig.length;
- outp = (char *) &dec_str.sig.text[0];
-
- if( dec_str.sgn != 0 ) printf( "-" );
-
- while( num_dig-- > 0 )
- {
- printf( "%c", *outp++ );
- }
- }
-
-
- void tst_compare3( wide *big1_ptr, wide *big2_ptr, wide *big3_ptr,
- wide *save1_ptr, wide *save2_ptr, wide *save3_ptr, short equal_test )
- {
- short tst1, tst2, tst3;
-
- printf( "\n" );
-
- if( equal_test )
- {
- tst1 = WideCompare( save1_ptr, big1_ptr );
- tst2 = WideCompare( save2_ptr, big2_ptr );
- tst3 = WideCompare( save3_ptr, big3_ptr );
- if( (tst1 == 0) && (tst2 == 0) && (tst3 == 0) )
- printf( "PASS" );
- else
- printf( "FAIL" );
- printf( ": WideAdd32(), WideSubtract32, WideCompare() == 0\n" );
- }
-
- tst1 = WideCompare( big1_ptr, big2_ptr );
- tst2 = WideCompare( big1_ptr, big3_ptr );
- tst3 = WideCompare( big2_ptr, big3_ptr );
- if( (tst1 < 0) && (tst2 < 0) && (tst3 < 0) )
- printf( "PASS" );
- else
- printf( "FAIL" );
- printf( ": %d, %d, %d, WideCompare() < 0\n", tst1, tst2, tst3 );
-
- tst1 = WideCompare( big3_ptr, big2_ptr );
- tst2 = WideCompare( big3_ptr, big1_ptr );
- tst3 = WideCompare( big2_ptr, big1_ptr );
- if( (tst1 > 0) && (tst2 > 0) && (tst3 > 0) )
- printf( "PASS" );
- else
- printf( "FAIL" );
- printf( ": %d, %d, %d, WideCompare() > 0\n\n", tst1, tst2, tst3 );
-
- }
-
-
- void tst_wide( wide *big_ptr, long hi, long lo, char *strp )
- {
- if( (big_ptr->lo == lo) && (big_ptr->hi == hi) )
- printf( "PASS" );
- else
- printf( "FAIL" );
- printf( ": 0x%lx, 0x%lx, %s\n", big_ptr->hi, big_ptr->lo, strp );
- }
-
- void tst_long( long actual, long expected, char *strp )
- {
- if( actual == expected )
- printf( "PASS" );
- else
- printf( "FAIL" );
- printf( ": actual 0x%lx, expected 0x%lx, %s\n", actual, expected, strp );
- }
-
- void __assertprint(char *filenam_ptr, char *linenum_ptr, char *expression_ptr)
- {
- printf( "--- assert ---\n" );
- printf( "%s, %s, %s\n", filenam_ptr, linenum_ptr, expression_ptr );
- }
-
- void __assert_custom(char *filenam_ptr, char *linenum_ptr, char *expression_ptr)
- {
- __assertprint( filenam_ptr, linenum_ptr, expression_ptr );
- }
-